﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SkinSharp;

namespace DataStudio
{
    public partial class Login : Form
    {
        public SkinH_Net skinh;
        public Login()
        {
            skinh = new SkinH_Net();
            skinh.Attach();
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = this.textBox1.Text;
            string password = string.IsNullOrEmpty(this.textBox2.Text) ? string.Empty : this.textBox2.Text;

            EAS.Data.Linq.DataEntityQuery<ACCOUNT> query
                = EAS.Data.Linq.DataEntityQuery<ACCOUNT>.Create();

            var p = from item in query
                    where item.LOGINID == username
                    //&& item.PASSWORD == EAS.Security.Cryptography.Password.Encrypt(password)
                    select item;
            MessageBox.Show(ToHexString(p.ToList()[0].PASSWORD));
            MessageBox.Show(EAS.Security.Cryptography.Password.Verify(password, p.ToList()[0].PASSWORD).ToString());

        }

        public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();

                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();
            }
            return hexString;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            myDelegate1 myDel = new myDelegate1(WaitForm.ShowWaitForm);
            myDel.BeginInvoke(null, null);
            this.DoWork();
        }

        private void DoWork()
        {
            Main main = new Main();
            main.ShowDialog();
            this.Hide();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBox1.Text) && e.KeyCode == Keys.Enter)
            {
                this.textBox2.Select();
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBox2.Text) && e.KeyCode == Keys.Enter)
            {
                this.button1.Select();
            }
        }
    }
}
